//
//  MSImagesSkimView.m
//  Weshare
//
//  Created by KevinCao on 16/3/19.
//  Copyright © 2016年 wxwl. All rights reserved.
//

#import "MSImagesSkimView.h"

@interface MSImagesSkimView ()
@property(nonatomic,strong)NSTimer* scrollTimer;
@end

@implementation MSImagesSkimView
@synthesize index,dataArray,scrollTimer;

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self)
    {
        self.m_TableView.pagingEnabled = NO;
        self.m_TableView.bounces = YES;
        self.m_TableView.backgroundColor = [UIColor whiteColor];
        UIView *headerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.height, 12)];
        headerView.backgroundColor = [UIColor whiteColor];
        self.m_TableView.tableHeaderView = headerView;
        
        UIView *footerView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.height, 7)];
        footerView.backgroundColor = [UIColor whiteColor];
        self.m_TableView.tableFooterView = footerView;
//        self.m_TableView.decelerationRate = 0.6;
//        [self startScrollTimer];
    }
    
    return self;
}

- (void)setDataArray:(NSArray *)imageArr
{
    dataArray = imageArr;
    self.m_TableView.hidden = !imageArr.count;
    [self.m_TableView reloadData];
    self.pageCtr.numberOfPages = imageArr.count;
    CGSize pointSize = [self.pageCtr sizeForNumberOfPages:imageArr.count];
    CGFloat pageCtrlLeft = (self.width-pointSize.width)/2.0;
    if (self.pageControlAlignment==PageControlAlignmentLeft)
    {
        pageCtrlLeft = 20;
    }
    else if (self.pageControlAlignment==PageControlAlignmentRight)
    {
        pageCtrlLeft = self.width-pointSize.width-20;
    }
    self.pageCtr.frame = CGRectMake(pageCtrlLeft, self.pageCtr.top, pointSize.width, self.pageCtr.height);
    self.pageCtr.currentPage = 0;
    index=0;
    self.tagLabel.text = [NSString stringWithFormat:@"1/%zi",imageArr.count];
}

-(void)startScrollTimer
{
    if (scrollTimer!=nil)
    {
        [scrollTimer invalidate];
        scrollTimer=nil;
    }
    
    scrollTimer=[NSTimer timerWithTimeInterval:0.01 target:self selector:@selector(autoSkimImgView) userInfo:nil repeats:YES];
    [[NSRunLoop currentRunLoop] addTimer:scrollTimer forMode:NSRunLoopCommonModes];
}

-(void)autoSkimImgView
{
    float scrollOffsetY = self.m_TableView.contentOffset.y+0.3;
    if (    scrollOffsetY >= (self.width-20)*(self.dataArray.count-1)-1   )
    {
        [scrollTimer invalidate];
        scrollTimer=nil;
        return;
    }
  
    self.m_TableView.contentOffset=CGPointMake( self.m_TableView.contentOffset.x,  scrollOffsetY);
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *idty = @"imgshowCell";
    ImgItemCell *cell = [tableView dequeueReusableCellWithIdentifier:idty];
    if (nil == cell)
    {
        cell = [[ImgItemCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:idty];
        cell.contentView.transform = CGAffineTransformMakeRotation(M_PI / 2);
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.contentView.backgroundColor = [UIColor whiteColor];
    }
    cell.rect = CGRectMake(0, 0, self.width-24, self.height);//图片位置
    cell.imgName = nil;
    propertyPicture* picInfo = [self.dataArray objectAtIndex:indexPath.row];
    
    cell.imgName = picInfo.path;
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:NO];
    if ([self.delegate respondsToSelector:@selector(tableView:didSelectRowAtIndexPath:)])
    {
        [self.delegate performSelector:@selector(tableView:didSelectRowAtIndexPath:) withObject:tableView withObject:indexPath];
    }
    if (self.delegate &&[self.delegate respondsToSelector:@selector(imagesSkimView:tappedIndex:)])
    {
        [self.delegate imagesSkimView:self tappedIndex:indexPath.row];
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return self.frame.size.width-20;//每个item宽度
}

- (void)scrollViewWillEndDragging:(UIScrollView *)scrollView withVelocity:(CGPoint)velocity targetContentOffset:(inout CGPoint *)targetContentOffset
{
    CGFloat pageSize = self.width-20;
    NSInteger page = roundf((*targetContentOffset).y / pageSize);
    CGFloat targetY = page*pageSize;
    targetContentOffset->y = targetY;
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (scrollTimer!=nil)
    {
        [scrollTimer invalidate];
        scrollTimer=nil;
    }
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
}



@end
